package com.project.microweatherservice.job;

import com.project.microweatherservice.service.LoadCityDataService;
import com.project.microweatherservice.service.WeatherDataService;
import com.project.microweatherservice.vo.City;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;

@Slf4j
public class WeatherDataSyncJob extends QuartzJobBean {

    @Autowired
    private LoadCityDataService loadCityDataService;
    @Autowired
    private WeatherDataService weatherDataService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("Start sync weather data job>>>>>>");
        List<City> cityList = loadCityDataService.loadData();
        if (CollectionUtils.isNotEmpty(cityList)) {
            for (City city : cityList) {
                weatherDataService.syncDataByCityId(city.getCityId());
            }
        }
        log.info("End sync weather data job>>>>>>");
    }
}
